﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SaunaClient.Model.Network;
using SaunaClient.Controller.Communication;

namespace SaunaClient.View
{
    public partial class LoginForm : Form
    {
        private ICommunicationManager communicationManager;
        private string loginId = "Anonymous";

        private const string ERROR_MESSAGE_USER_AUTHENTICATION_FAILED = "User Authentication Error";
        private const string ERROR_MESSAGE_SERVER_CONNECTION_FAILED = "Failed to Connect to Server";

        public LoginForm(ICommunicationManager communicationManager)
        {
            InitializeComponent();

            // Set user authentication controller
            this.communicationManager = communicationManager;
        }

        private void ButtonLogin_Click(object sender, EventArgs e)
        {

            // Request user authentication
            UserAuthenticationStatus result = communicationManager.AuthenticationRequest(textBoxUserId.Text, textBoxPassword.Text);
            if (result.GetStatus() == UserAuthenticationStatus.Status.SUCCESS)
            {
                loginId = textBoxUserId.Text;
                DialogResult = DialogResult.OK;
                Close();
            }
            else if (result.GetStatus() == UserAuthenticationStatus.Status.CONNECT_ERROR)
            {
                // Server Connection Error
                Console.Beep();
                labelErrorMessage.Text = ERROR_MESSAGE_SERVER_CONNECTION_FAILED;
            }
            else
            {
                // User Authentication Error
                Console.Beep();
                labelErrorMessage.Text = ERROR_MESSAGE_USER_AUTHENTICATION_FAILED;
            }
        }

        public string LoginId
        {
            get { return loginId; }
            set { loginId = value; }
        }

    }
}
